in_text=$1
server_port=$2
num_bin_path=$3
use_txt_smooth=$4
ssc_bin_path=$5
use_ssc=$6
use_inverse_txt=$7
nthread=$8
post_cfg=$9
mgc_bin_path=${10}
ssc_wav_path=${11}
nj=10
[ -z $in_text ] && echo "$in_text is empty" && exit 1;
[ -z $server_port ] && echo "server_port is empty" && exit 1;
[ -z $num_bin_path ] && echo "num.bin path is empty" && exit 1;
[ -z $use_txt_smooth ] && echo "use_txt_smooth is empty" && exit 1;
[ -z $ssc_bin_path ] && echo "ssc bin path is empty" && exit 1;
[ -z $use_ssc ] && echo "use_ssc is empty" && exit 1;
[ -z $nthread ] && echo "nthread is empty" && exit 1;
[ -z $post_cfg ] && echo "post_cfg is empty" && exit 1;
[ -z $use_inverse_txt ] && echo "use_inverse_txt is empty" && exit 1;
[ -z $mgc_bin_path ] && echo "mgc_bin_path is empty" && exit 1;
[ -z $ssc_wav_path ] && echo "ssc_wav_path is empty" && exit 1;
#if [ -z $use_txt_smooth ]; then
#    use_txt_smooth=1
#fi

outdir=`head -n 1 $in_text | awk '{num=split($4,a,"/");aa=a[1];for(i=2;i<num-1;i++){aa=aa"/"a[i];}printf aa;}'`

exec 1>> $outdir/post.log
exec 2>> $outdir/post.log

#set -x

echo $in_text
echo $outdir

[ ! -f $outdir/align.res ] && echo "align.res does not exist" && exit 1;
[ ! -f $outdir/st.list ] && echo "st.list does not exist" && exit 1;

[ ! -d $outdir/align ] && mkdir $outdir/align;
[ ! -d $outdir/mlfs ] && mkdir $outdir/mlfs;
[ ! -d $outdir/sym ] && mkdir $outdir/sym;
[ ! -d $outdir/log  ] && mkdir $outdir/log;

cp $outdir/align.res $outdir/align/
cp $outdir/st.list $outdir/align/

function getTiming() {
    start=$1
    end=$2
    module_name=$3
    start_s=$(echo $start | cut -d '.' -f 1)
    start_ns=$(echo $start | cut -d '.' -f 2)
    end_s=$(echo $end | cut -d '.' -f 1)
    end_ns=$(echo $end | cut -d '.' -f 2)
    time=$(( ( 10#$end_s - 10#$start_s ) * 1000 + ( 10#$end_ns / 1000000 - 10#$start_ns / 1000000 ) ))
    echo "$module_name $time"
}

function getTimestamp() {
    end=$1
    module_name=$2
    end_s=$(echo $end | cut -d '.' -f 1)
    end_ns=$(echo $end | cut -d '.' -f 2)
    time=$(( ( 10#$end_s ) * 1000 + ( 10#$end_ns / 1000000 ) ))
    echo "$module_name $time"
}

if false; then
#res/get_meta_fuse -i $in_text -o $outdir/align/meta.fuse.new
awk -F/ '{gsub(/\.wav/,"",$NF);print $NF;}' $in_text >$outdir/mlfs/text
res/text2mlf $outdir/mlfs/text $outdir/mlfs/res.mlf

awk '{aa=$4;gsub(/\.wav/,"\.plp",aa);print $4" "aa;}' $in_text >$outdir/align/wav2plp.scp

wav_num=`wc -l $outdir/align/wav2plp.scp | awk '{print $1}'`
if [ $wav_num -ge $nj ]; then
    res/cutlists.sh $nj $outdir/align/wav2plp.scp
	res/run.pl JOB=1:$nj  $outdir/log/wav2plp_JOB.log \
		res/HCopy -C res/config.wb.13dimplp.raw.16k -S $outdir/align/wav2plp_JOB.scp
	find $outdir/wav -name "*.plp" >$outdir/align/plp.scp
	res/cutlists.sh $nj $outdir/align/plp.scp
	res/run.pl JOB=1:$nj $outdir/log/align_JOB.log \
		res/HVite -A -D -T 1 -a -f -i $outdir/align/align_JOB.mlf -H res/MMF -y lab -C res/config.herest -t 500.0 500.0 1500.0 \
		    -I $outdir/mlfs/res.mlf -b sil -S $outdir/align/plp_JOB.scp res/comm.vocab.118k.v6.sil.dict res/tri.xwrd.cluster.list
    cat $outdir/align/align_*.mlf >$outdir/align/align.mlf
else
    res/HCopy -C res/config.wb.13dimplp.raw.16k -S $outdir/align/wav2plp.scp
    find $outdir/wav -name "*.plp" >$outdir/align/plp.scp

    res/HVite -A -D -T 1 -a -f -i $outdir/align/align.mlf -H res/MMF -y lab -C res/config.herest -t 500.0 \
	    -I $outdir/mlfs/res.mlf -b sil -S $outdir/align/plp.scp res/comm.vocab.118k.v6.sil.dict res/tri.xwrd.cluster.list
fi

awk '{print $4" "$1}' $in_text | awk -F/ '{gsub(/\.wav/,"\.lab",$NF);print $NF}' >$outdir/align/st.list

awk 'NR==FNR{a[$1]=$2;}
    NR!=FNR{
	    if($1~/\.lab/){
			num=split($1,b,"/");gsub(/\"/,"",b[num]);st=a[b[num]];
		}else if($1=="\."){
			print;
		}else if($1~/MLF/){}
		else{ $1=$1/10000+st;$2=$2/10000+st;print;}
	}' $outdir/align/st.list $outdir/align/align.mlf | \
awk 'BEGIN{st=0;et=0;word="";}{
	if(NF==5){
		if(et!=0){
			printf("%ld %ld %s\n",st,et,word);}st=$1;word=$5;
		}else if($1=="\."){
			st=0;et=0;word="";
		}else{et=$2}}' | grep -v "sil" | sort -k 1n > $outdir/align/align.res

python res/mlf2seg.py $outdir/align/align.mlf $outdir/align/st.list 0.25 > $outdir/align/diarization.mlf
#res/get_align_res -a $outdir/align/align.res -m $in_text -o $outdir/align/align.res.new
#cp $outdir/align/align.res $outdir/align/align.res.single
#cp $outdir/align/align.res.new $outdir/align/align.res
fi

python res/get_diarization.py $outdir/align/align.res 0.25 > $outdir/align/diarization.mlf

: > $outdir/log/log_time

if (($use_ssc != 0)); then
#start_diarization=$(date +%s.%N)
echo "--- start diarization ---"
cp $outdir/align/diarization.mlf $outdir/align/diarization.mlf.bak
cp $outdir/align/st.list $outdir/align/st.list.bak
res/ssc_engine -ssc $ssc_bin_path -wav $outdir/wav/$ssc_wav_path -f $outdir/align/diarization.mlf -p $server_port -n $nthread -spk $use_ssc
python res/get_alignres.py $outdir/align/diarization.mlf $outdir/align/align.res 3000 > $outdir/align/align.res.uid
cp $outdir/align/align.res $outdir/align/align.res.bak
cp $outdir/align/align.res.uid $outdir/align/align.res
python res/get_stlist.py $outdir/align/align.res 3000 > $outdir/align/st.list
end_diarization=$(date +%s.%N)
time_diarization=$(getTimestamp $end_diarization diarizated_t)
echo $time_diarization >> $outdir/log/log_time
else
    cp $outdir/align/align.res $outdir/align/align.res.bak
    cp $outdir/align/st.list $outdir/align/st.list.bak
    python res/get_alignres2.py $outdir/align/align.res > $outdir/align/align.res.uid
    cp $outdir/align/align.res.uid $outdir/align/align.res
    python res/get_stlist2.py $outdir/align/align.res $outdir/align/st.list 3000 > $outdir/align/st.list.new
    mv $outdir/align/st.list.new $outdir/align/st.list
fi

if (($use_txt_smooth == 1)); then
#start_smooth=$(date +%s.%N)
echo "--- start txt smooth ---"

stdir=$outdir/align/st.list
align_ori=$outdir/align/align.res
out_dir=$outdir/align/smooth_tmp
smooth_final=$outdir/align/align.smooth

mkdir -p $out_dir
input=$out_dir/align.temp
true &&{
    if (($use_ssc != 0)); then
        python res/single_test.py $stdir $align_ori > $input
    else
        python res/get_no_diarization_input.py $align_ori 3000 > $input
    fi
}
ar=($(cat $input))
#echo ${ar[0]}
echo ${#ar[@]}
true &&
{
index=0
while (($index < ${#ar[@]}))
do
    cnt=$nthread
    remain=$((${#ar[@]} - $index))
    if (($cnt > $remain)); then
        cnt=$remain
    fi
    for jobs in `seq $cnt`
    do
        #echo ${ar[index]}
        #res/smooth_engine  -p $server_port -t 30000 -s ${ar[index]} -o $out_dir/$index &
        res/txtpost_offline -c $post_cfg -s ${ar[index]} -p $server_port -txt_smooth 1 -o $out_dir/$index &
        index=$((index+1))
    done
    wait
done
}

true &&{
    for index in `seq 0 ${#ar[@]}`
    do
        cat $out_dir/$index
    done > $out_dir/all
awk '{gsub("~"," ");print $0}' $out_dir/all > $smooth_final
}
#cp $outdir/align/align.res $outdir/align/align.res.bak
cp $outdir/align/align.smooth $outdir/align/align.res
end_smooth=$(date +%s.%N)
time_smooth=$(getTimestamp $end_smooth smoothed_t)
echo $time_smooth >> $outdir/log/log_time
fi



#start_punctuation=$(date +%s.%N)
echo "--- start predict punctuation mark ---"

stdir=$outdir/align/st.list
align_ori=$outdir/align/align.res
out_dir=$outdir/align/punctuation_tmp
punctuation_final=$outdir/align/align.punctuation

mkdir -p $out_dir
input=$out_dir/align.temp
true &&{
    if (($use_ssc != 0)); then
        python res/get_punctuation_input.py $stdir $align_ori > $input
    else
        python res/get_no_diarization_input_punc.py $stdir $align_ori 3000 > $input
    fi
}
ar=($(cat $input))
echo ${#ar[@]}
true &&
{
index=0
while (($index < ${#ar[@]}))
do
    cnt=$nthread
    remain=$((${#ar[@]} - $index))
    if (($cnt > $remain)); then
        cnt=$remain
    fi
    for jobs in `seq $cnt`
    do
        #echo ${ar[index]}
        #res/punctuation_engine  -p $server_port -t 30000 -s ${ar[index]} -o $out_dir/$index &
        res/txtpost_offline -c $post_cfg -s ${ar[index]} -p $server_port -nn_punc 1 -o $out_dir/$index &
        index=$((index+1))
    done
    wait
done
}

true &&{
    for index in `seq 0 ${#ar[@]}`
    do
        cat $out_dir/$index
    done > $out_dir/all
awk '{gsub("~"," ");print $0}' $out_dir/all > $punctuation_final
}
cp $outdir/align/align.punctuation $outdir/sym/align_sym

awk 'BEGIN{stf=1;word="";st=0;}{
		if(stf==1){st=$1;stf=0;}
		num=split(word,a,"");
		num2=split($4,b,"");
		if(a[num]~/[a-zA-Z]/&&b[1]~/[a-zA-Z]/){word=word" "$4;}
		else{word=word""$4;}
		et=$2;
		if(NF==5){
			print st" "et" "$3" "word""$NF;
			word="";stf=1;st=0;et=0
		}
	}END{if(stf==0){print st" "et" "$3" "word;}}' $outdir/sym/align_sym >$outdir/sym/align_sym.res

#awk 'BEGIN{stf=1;word="";st=0;}{
#		if(stf==1){st=$1;stf=0;}
#		word=word" "$3;et=$2;
#		if(NF==4){
#			print st" "et" 0 "word""$NF;
#			word="";stf=1;st=0;et=0
#		}
#	}END{if(stf==0){print st" "et" 0 "word;}}' $outdir/sym/align_sym >$outdir/sym/align_sym.res
end_punctuation=$(date +%s.%N)
time_punctuation=$(getTimestamp $end_punctuation punctuated_t)
echo $time_punctuation >> $outdir/log/log_time


cp $outdir/sym/align_sym.res $outdir/sym/final
if (($use_inverse_txt == 1)); then
#start_inverse_txt=$(date +%s.%N)
echo "--- start inverse text transform ---"

# awk '{print $NF;}' $outdir/sym/align_sym.res >$outdir/sym/itt_pre
# awk '{$NF="";print;}' $outdir/sym/align_sym.res >$outdir/sym/itt_left
luajit res/semantic_v3_lua_lexnum/src/test_new_itn.lua -bad res/semantic_v3_lua_lexnum/bin/num_badcase.bin -b1 res/semantic_v3_lua_lexnum/bin/num_rule1.bin \
  -b2 res/semantic_v3_lua_lexnum/bin/num_rule2.bin -p $server_port -v res/semantic_v3_lua_lexnum/bin/vocab.txt -i $outdir/sym/align_sym.res -o $outdir/sym/final
# paste $outdir/sym/itt_left $outdir/sym/itt_done >$outdir/sym/final
end_inverse_txt=$(date +%s.%N)
time_inverse_txt=$(getTimestamp $end_inverse_txt inversed_t)
echo $time_inverse_txt >> $outdir/log/log_time
fi

if [ $mgc_bin_path != "0" ]; then
echo "--- start sensitive word ---"

cp $outdir/sym/final $outdir/sym/final_itn
luajit res/semantic_v3_lua_sensitive/src/test_lex_sensitive_wds.lua -b $mgc_bin_path \
  -i $outdir/sym/final_itn -o $outdir/sym/final
end_sensitive_word=$(date +%s.%N)
time_sensitive_word=$(getTimestamp $end_sensitive_word sensitived_t)
echo $time_sensitive_word >> $outdir/log/log_time
fi

cp $outdir/sym/final $outdir/sym/final.bak
python res/get_allfinal.py $in_text $outdir/sym/final.bak > $outdir/sym/final

if false; then
if (($use_ssc != 0)); then
#start_diarization=$(date +%s.%N)
echo "--- start diarization ---"
cp $outdir/sym/final $outdir/sym/final.bak
cp $outdir/align/diarization.mlf $outdir/align/diarization.mlf.bak
res/ssc_engine -ssc $ssc_bin_path -wav $outdir/wav/0.wav -f $outdir/align/diarization.mlf -n $nthread -spk $use_ssc
python res/get_final.py $outdir/align/diarization.mlf $outdir/align/align.res 3000 > $outdir/sym/final
end_diarization=$(date +%s.%N)
time_diarization=$(getTimestamp $end_diarization diarizated_t)
echo $time_diarization >> $outdir/log/log_time
fi
fi
